package com.example.app;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.ClipData;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.LayerDrawable;
import android.os.Build;
import android.os.Bundle;
import android.view.DragEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewManager;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;


import java.security.SecureRandom;
import java.text.DecimalFormat;
import java.util.Arrays;
import java.util.Stack;

@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public class MainActivity extends Activity implements View.OnTouchListener, View.OnDragListener {

    private static SecureRandom randomNumbers = new SecureRandom();

    static int point = 0;   // point if no win/loss on first roll
    static int sumOfDice, sum, die1, die2;
    static public int passBetRegion;
    static public int passOddsBet;
    static public int dontPassBetRegion;
    static public int dontPassOddsBet;
    static public int fieldsBet;
    static public int comeBet;
    static public int dontComeBet;
    static public int lay4, lay5, lay6, lay8,lay9, lay10;
    static public int point4, point5, point6, point8,point9, point10;
    static public int place4, place5, place6, place8,place9, place10;
    static public int totalPassBetRegion;
    static public int totalDontPassBetRegion;
    static public int totalComeBetRegion;
    static public int totalDontComeBetRegion;
    static public int totalFieldsBet;
    static public int chipSelected;
    static public int totalBet;
    static public int totalDontPassOddsBet;
    static public int totalPassOddsBet;
    static public boolean chipExistedOnRegion;
    static public double money = 500.00;
    static boolean comeOutRoll = true, hasBet, hasPoint, hasFieldsBet,
            hasDontPassBetRegion, hasPassBetRegion, hasPassOddsBet, hasDontPassOddsBet;
    Integer[] pointValues={4, 5, 6, 8, 9, 10};
    Integer[] fieldValues={2, 3, 4, 9, 10, 11, 12};
    private static Status gameStatus;
    private static View selectedChip;
    private Stack<ImageView> passRegionBetChipHolder =  new Stack<>();
    private Stack<ImageView> passRegionOddsBetChipHolder =  new Stack<>();
    private Stack<ImageView> dontPassRegionBetChipHolder =  new Stack<>();
    private Stack<ImageView> dontPassRegionOddsBetChipHolder =  new Stack<>();
    private Stack<ImageView> fieldsRegionBetChipHolder =  new Stack<>();
    private Stack<ImageView> comeRegionBetChipHolder =  new Stack<>();
    private Stack<ImageView> comeRegionOddsBetChipHolder =  new Stack<>();
    private Stack<ImageView> dontComeRegionBetChipHolder =  new Stack<>();
    private Stack<ImageView> dontComeRegionOddsBetChipHolder =  new Stack<>();

    private Stack<ImageView> lay4RegionBetChipHolder =  new Stack<>();
    private Stack<ImageView> lay4RegionOddsBetChipHolder =  new Stack<>();
    private Stack<ImageView> lay5RegionBetChipHolder =  new Stack<>();
    private Stack<ImageView> lay5RegionOddsBetChipHolder =  new Stack<>();
    private Stack<ImageView> lay6RegionBetChipHolder =  new Stack<>();
    private Stack<ImageView> lay6RegionOddsBetChipHolder =  new Stack<>();
    private Stack<ImageView> lay8RegionBetChipHolder =  new Stack<>();
    private Stack<ImageView> lay8RegionOddsBetChipHolder =  new Stack<>();
    private Stack<ImageView> lay9RegionBetChipHolder =  new Stack<>();
    private Stack<ImageView> lay9RegionOddsBetChipHolder =  new Stack<>();
    private Stack<ImageView> lay10RegionBetChipHolder =  new Stack<>();
    private Stack<ImageView> lay10RegionOddsBetChipHolder =  new Stack<>();

    private Stack<ImageView> point4RegionBetChipHolder =  new Stack<>();
    private Stack<ImageView> point4RegionOddsBetChipHolder =  new Stack<>();
    private Stack<ImageView> point5RegionBetChipHolder =  new Stack<>();
    private Stack<ImageView> point5RegionOddsBetChipHolder =  new Stack<>();
    private Stack<ImageView> point6RegionBetChipHolder =  new Stack<>();
    private Stack<ImageView> point6RegionOddsBetChipHolder =  new Stack<>();
    private Stack<ImageView> point8RegionBetChipHolder =  new Stack<>();
    private Stack<ImageView> point8RegionOddsBetChipHolder =  new Stack<>();
    private Stack<ImageView> point9RegionBetChipHolder =  new Stack<>();
    private Stack<ImageView> point9RegionOddsBetChipHolder =  new Stack<>();
    private Stack<ImageView> point10RegionBetChipHolder =  new Stack<>();
    private Stack<ImageView> point10RegionOddsBetChipHolder =  new Stack<>();

    private Stack<ImageView> place4RegionBetChipHolder =  new Stack<>();
    private Stack<ImageView> place4RegionOddsBetChipHolder =  new Stack<>();
    private Stack<ImageView> place5RegionBetChipHolder =  new Stack<>();
    private Stack<ImageView> place5RegionOddsBetChipHolder =  new Stack<>();
    private Stack<ImageView> place6RegionBetChipHolder =  new Stack<>();
    private Stack<ImageView> place6RegionOddsBetChipHolder =  new Stack<>();
    private Stack<ImageView> place8RegionBetChipHolder =  new Stack<>();
    private Stack<ImageView> place8RegionOddsBetChipHolder =  new Stack<>();
    private Stack<ImageView> place9RegionBetChipHolder =  new Stack<>();
    private Stack<ImageView> place9RegionOddsBetChipHolder =  new Stack<>();
    private Stack<ImageView> place10RegionBetChipHolder =  new Stack<>();
    private Stack<ImageView> place10RegionOddsBetChipHolder =  new Stack<>();
    private boolean hasComeBet, hasDontComeBet;
    private View previousRegion;


    @Override
    public boolean onTouch(View view, MotionEvent motionEvent) {
        if (motionEvent.getAction() == MotionEvent.ACTION_MOVE) {
            previousRegion = (View) view.getParent();
            View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
            view.startDrag(null, shadowBuilder, view, 0);

            chipExistedOnRegion = false;
            if (view.getBackground().equals(findViewById(R.id.chip1ImageView).getBackground())) {
                chipSelected = 1;
            } else if (view.getBackground().equals(findViewById(R.id.chip5ImageView).getBackground())) {
                chipSelected = 5;
            } else if (view.getBackground().equals(findViewById(R.id.chip25ImageView).getBackground())) {
                chipSelected = 25;
            } else if (view.getBackground().equals(findViewById(R.id.chip100ImageView).getBackground())) {
                chipSelected = 100;
            } else if (view.getBackground().equals(findViewById(R.id.chip500ImageView).getBackground())) {
                chipSelected = 500;
            }
            if(!view.getParent().equals(findViewById(R.id.chipGridLayout)) && chipSelected != 0){
                chipExistedOnRegion = true;
                view.setVisibility(View.INVISIBLE);
            }

            return true;
        } else {
            return false;
        }
    }


    @Override
    public boolean onDrag(View view, DragEvent dragEvent) {
        // Dropped, reassign View to ViewGroup
        View v = (View) dragEvent.getLocalState();
        ViewGroup container = (ViewGroup) view;

        if (dragEvent.getAction()==DragEvent.ACTION_DROP) {
            // Added the following to copy the old view's bitmap to a new ImageView:
            ImageView oldView = (ImageView) v;
            ImageView newView = new ImageView(getApplicationContext());
            newView.setBackground(oldView.getBackground());

            if (chipExistedOnRegion) {
                if (!previousRegion.equals(container)) {
                    ((ViewManager)v.getParent()).removeView(v);
                    removeChipFromRegion();
                }
            }
            newView.setVisibility(View.VISIBLE);
            newView.setLongClickable(true);
            newView.setOnTouchListener(this);
            if (container.equals(findViewById(R.id.fieldsBetRegion))) {
                if(!hasFieldsBet && !chipExistedOnRegion) {
                    fieldsRegionBetChipHolder.push(newView);
                    for (int i = 0; i < 6; i++) {
                        if (i == 5) {
                            container.addView(fieldsRegionBetChipHolder.peek());
                        } else {
                            ImageView blankView = new ImageView(getApplicationContext());
                            blankView.setBackground(oldView.getBackground());
                            blankView.setVisibility(View.INVISIBLE);
                            container.addView(blankView);
                        }
                    }
                }else if(hasFieldsBet && !chipExistedOnRegion) {
                    fieldsRegionBetChipHolder.push(newView);
                    fieldsRegionBetChipHolder.peek();
                }else if (!hasFieldsBet && chipExistedOnRegion) {
                    if (!previousRegion.equals(container)) {
                        fieldsRegionBetChipHolder.push(newView);
                        container.addView(fieldsRegionBetChipHolder.peek());
                    } else {
                        container.addView(fieldsRegionBetChipHolder.peek());
                        return true;
                    }
                }else if (hasFieldsBet && chipExistedOnRegion) {
                    if (!previousRegion.equals(container)) {
                        fieldsRegionBetChipHolder.push(newView);
                        container.addView(fieldsRegionBetChipHolder.peek());
                    } else {
                        container.addView(fieldsRegionBetChipHolder.peek());
                        return true;
                    }
                }
                fieldsBetActivity();
            /////////////////////////////
            ///// DONT COME REGION //////
            /////////////////////////////
            }else if (container.equals(findViewById(R.id.dontComeBetRegion))) {
                if(!hasDontComeBet && !chipExistedOnRegion) {
                    dontComeRegionBetChipHolder.push(newView);
                    for (int i = 0; i < 6; i++) {
                        if (i == 5) {
                            container.addView(dontComeRegionBetChipHolder.peek());
                        } else {
                            ImageView blankView = new ImageView(getApplicationContext());
                            blankView.setBackground(oldView.getBackground());
                            blankView.setVisibility(View.INVISIBLE);
                            container.addView(blankView);
                        }
                    }
                }else if(hasDontComeBet && !chipExistedOnRegion) {
                    dontComeRegionBetChipHolder.push(newView);
                    dontComeRegionBetChipHolder.peek();
                }else if (!hasDontComeBet && chipExistedOnRegion) {
                    dontComeRegionBetChipHolder.push(newView);
                    container.addView(dontComeRegionBetChipHolder.peek());
                }else if (hasDontComeBet && chipExistedOnRegion) {
                    dontComeRegionBetChipHolder.push(newView);
                    dontComeRegionBetChipHolder.peek();
                }
                DontComeBetActivity();
            ////////////////////////////
            /////   COME REGION   //////
            ////////////////////////////
            }else if (container.equals(findViewById(R.id.comeBetRegion))) {
                if(!hasComeBet && hasPoint) {
                    comeRegionBetChipHolder.push(newView);
                    for (int i = 0; i < 6; i++) {
                        if (i == 5) {
                            container.addView(comeRegionBetChipHolder.peek());
                        } else {
                            ImageView blankView = new ImageView(getApplicationContext());
                            blankView.setBackground(oldView.getBackground());
                            blankView.setVisibility(View.INVISIBLE);
                            container.addView(blankView);
                        }
                    }
                }else if(hasComeBet && hasPoint) {
                    comeRegionBetChipHolder.push(newView);
                    comeRegionBetChipHolder.peek();
                }
                ComeBetActivity();
            ////////////////////////////
            /////   PASS REGION   //////
            ////////////////////////////
            }else if (container.equals(findViewById(R.id.passBetRegion))) {
                if (!chipExistedOnRegion && !hasPoint && !hasPassBetRegion && !hasPassOddsBet) {
                    passRegionBetChipHolder.push(newView);
                    for (int i = 0; i < 6; i++) {
                        if (i == 5) {
                            container.addView(passRegionBetChipHolder.peek());
                        } else {
                            ImageView blankView = new ImageView(getApplicationContext());
                            blankView.setBackground(oldView.getBackground());
                            blankView.setVisibility(View.INVISIBLE);
                            container.addView(blankView);
                        }
                    }
                } else if (!chipExistedOnRegion && !hasPoint && hasPassBetRegion && !hasPassOddsBet) {
                    Resources r = getResources();
                    Drawable[] layers = new Drawable[2];
                    layers[0] = oldView.getBackground();
                    layers[1] = newView.getBackground();
                    LayerDrawable layerDrawable = new LayerDrawable(layers);
                    newView.setImageDrawable(layerDrawable);
                    container.removeView(view);
                    container.addView(newView);
                }else if (!chipExistedOnRegion && hasPoint && hasPassBetRegion && !hasPassOddsBet) {
                    passRegionOddsBetChipHolder.push(newView);
                    container.addView(passRegionOddsBetChipHolder.peek());
                }else if (!chipExistedOnRegion && hasPoint && hasPassBetRegion && hasPassOddsBet) {
                    passRegionOddsBetChipHolder.push(newView);
                    passRegionOddsBetChipHolder.peek();
                }else if (chipExistedOnRegion && !hasPoint && !hasPassBetRegion && !hasPassOddsBet) {
                    passRegionBetChipHolder.push(newView);
                    for (int i = 0; i < 6; i++) {
                        if (i == 5) {
                            container.addView(passRegionBetChipHolder.peek());
                        } else {
                            ImageView blankView = new ImageView(getApplicationContext());
                            blankView.setBackground(oldView.getBackground());
                            blankView.setVisibility(View.INVISIBLE);
                            container.addView(blankView);
                        }
                    }

                } else if (chipExistedOnRegion && !hasPoint && hasPassBetRegion && !hasPassOddsBet) {
                    if (!previousRegion.equals(container)) {
                        passRegionBetChipHolder.push(newView);
                        passRegionBetChipHolder.peek();
                        v.setVisibility(View.VISIBLE);
                    } else {
                        v.setVisibility(View.VISIBLE);
                        return true;
                    }
                }else if (chipExistedOnRegion && hasPoint && hasPassBetRegion && !hasPassOddsBet) {
                    passRegionOddsBetChipHolder.push(newView);
                    container.addView(passRegionOddsBetChipHolder.peek());

                }else if (chipExistedOnRegion && hasPoint && hasPassBetRegion && hasPassOddsBet) {
                    if (!previousRegion.equals(container)) {
                        passRegionOddsBetChipHolder.push(newView);
                        passRegionOddsBetChipHolder.peek();
                        v.setVisibility(View.VISIBLE);
                    } else {
                        v.setVisibility(View.VISIBLE);
                        return true;
                    }
                }

                passBetRegionActivity();
            ////////////////////////////\\\\
            /////   DONT PASS REGION   /////
            ////////////////////////////\\\\
            }else if (container.equals(findViewById(R.id.dontPassBetRegion))) {
                if (!chipExistedOnRegion && !hasPoint && !hasDontPassBetRegion && !hasDontPassOddsBet) {
                    dontPassRegionBetChipHolder.push(newView);
                    for (int i = 0; i < 6; i++) {
                        if (i == 5) {
                            container.addView(dontPassRegionBetChipHolder.peek());
                        } else {
                            ImageView blankView = new ImageView(getApplicationContext());
                            blankView.setBackground(oldView.getBackground());
                            blankView.setVisibility(View.INVISIBLE);
                            container.addView(blankView);
                        }
                    }
                } else if (!chipExistedOnRegion && !hasPoint && hasDontPassBetRegion && !hasDontPassOddsBet) {
                    dontPassRegionBetChipHolder.push(newView);
                    dontPassRegionBetChipHolder.peek();

                }else if (!chipExistedOnRegion && hasPoint && hasDontPassBetRegion && !hasDontPassOddsBet) {
                    dontPassRegionOddsBetChipHolder.push(newView);
                    container.addView(dontPassRegionOddsBetChipHolder.peek());

                }else if (!chipExistedOnRegion && hasPoint && hasDontPassBetRegion && hasDontPassOddsBet) {
                    dontPassRegionOddsBetChipHolder.push(newView);
                    dontPassRegionOddsBetChipHolder.peek();

                }else if (chipExistedOnRegion && !hasPoint && !hasDontPassBetRegion && !hasDontPassOddsBet) {
                    dontPassRegionBetChipHolder.push(newView);
                    for (int i = 0; i < 6; i++) {
                        if (i == 5) {
                            container.addView(dontPassRegionBetChipHolder.peek());
                        } else {
                            ImageView blankView = new ImageView(getApplicationContext());
                            blankView.setBackground(oldView.getBackground());
                            blankView.setVisibility(View.INVISIBLE);
                            container.addView(blankView);
                        }
                    }


                } else if (chipExistedOnRegion && !hasPoint && hasDontPassBetRegion && !hasDontPassOddsBet) {
                    if (!previousRegion.equals(container)) {
                        dontPassRegionBetChipHolder.push(newView);
                        dontPassRegionBetChipHolder.peek();
                        v.setVisibility(View.VISIBLE);
                    } else {
                        v.setVisibility(View.VISIBLE);
                        return true;
                    }

                }else if (chipExistedOnRegion && hasPoint && hasDontPassBetRegion && !hasDontPassOddsBet) {
                    dontPassRegionOddsBetChipHolder.push(newView);
                    container.addView(dontPassRegionOddsBetChipHolder.peek());

                }else if (chipExistedOnRegion && hasPoint && hasDontPassBetRegion && hasDontPassOddsBet) {
                    if (!previousRegion.equals(container)) {
                        dontPassRegionOddsBetChipHolder.push(newView);
                        dontPassRegionOddsBetChipHolder.peek();
                        v.setVisibility(View.VISIBLE);
                    } else {
                        v.setVisibility(View.VISIBLE);
                        return true;
                    }
                }

                dontPassBetRegionActivity();

            }else if (container.equals(findViewById(R.id.lay10))) {

            }else if (container.equals(findViewById(R.id.lay9))) {

            }else if (container.equals(findViewById(R.id.lay8))) {

            }else if (container.equals(findViewById(R.id.lay6))) {

            }else if (container.equals(findViewById(R.id.lay5))) {

            }else if (container.equals(findViewById(R.id.lay4))) {

            }else if (container.equals(findViewById(R.id.point10))) {

            }else if (container.equals(findViewById(R.id.point9))) {

            }else if (container.equals(findViewById(R.id.point8))) {

            }else if (container.equals(findViewById(R.id.point6))) {

            }else if (container.equals(findViewById(R.id.point5))) {

            }else if (container.equals(findViewById(R.id.point4))) {

            }else if (container.equals(findViewById(R.id.place10))) {

            }else if (container.equals(findViewById(R.id.place9))) {

            }else if (container.equals(findViewById(R.id.place8))) {

            }else if (container.equals(findViewById(R.id.place6))) {

            }else if (container.equals(findViewById(R.id.place5))) {

            }else if (container.equals(findViewById(R.id.place4))) {

            }
        }else if (dragEvent.getAction()==DragEvent.ACTION_DRAG_ENDED) {
            if (container.equals(findViewById(R.id.relLayout))) {
                money = money - chipSelected;
                totalBet -= chipSelected;
                if (v.getParent().equals(findViewById(R.id.fieldsBetRegion))) {
                    fieldsRegionBetChipHolder.pop();
                    fieldsRegionBetChipHolder.peek();
                }else if (v.getParent().equals(findViewById(R.id.dontComeBetRegion))) {
                    dontComeRegionBetChipHolder.pop();
                    dontComeRegionBetChipHolder.peek();
                }else if (v.getParent().equals(findViewById(R.id.comeBetRegion))) {
                    comeRegionBetChipHolder.pop();
                    comeRegionBetChipHolder.peek();
                }else if (v.getParent().equals(findViewById(R.id.passBetRegion))) {
                    passRegionBetChipHolder.pop();
                    passRegionBetChipHolder.peek();
                }else if (v.getParent().equals(findViewById(R.id.dontPassBetRegion))) {
                    dontPassRegionBetChipHolder.pop();
                    dontPassRegionBetChipHolder.peek();
                }else if (v.getParent().equals(findViewById(R.id.lay10))) {

                }else if (v.getParent().equals(findViewById(R.id.lay9))) {

                }else if (v.getParent().equals(findViewById(R.id.lay8))) {

                }else if (v.getParent().equals(findViewById(R.id.lay6))) {

                }else if (v.getParent().equals(findViewById(R.id.lay5))) {

                }else if (v.getParent().equals(findViewById(R.id.lay4))) {

                }else if (v.getParent().equals(findViewById(R.id.point10))) {

                }else if (v.getParent().equals(findViewById(R.id.point9))) {

                }else if (v.getParent().equals(findViewById(R.id.point8))) {

                }else if (v.getParent().equals(findViewById(R.id.point6))) {

                }else if (v.getParent().equals(findViewById(R.id.point5))) {

                }else if (v.getParent().equals(findViewById(R.id.point4))) {

                }else if (v.getParent().equals(findViewById(R.id.place10))) {

                }else if (v.getParent().equals(findViewById(R.id.place9))) {

                }else if (v.getParent().equals(findViewById(R.id.place8))) {

                }else if (v.getParent().equals(findViewById(R.id.place6))) {

                }else if (v.getParent().equals(findViewById(R.id.place5))) {

                }else if (v.getParent().equals(findViewById(R.id.place4))) {

                }
            }
        }
        return true;
    }

    private void removeChipFromRegion() {

        if (previousRegion.equals(findViewById(R.id.fieldsBetRegion))) {
            fieldsRegionBetChipHolder.pop();
            if (!fieldsRegionBetChipHolder.empty()) {
                fieldsRegionBetChipHolder.peek();
            }
            removeFieldBetFromRegion();
        }else if (previousRegion.equals(findViewById(R.id.dontComeBetRegion))) {
            dontComeRegionBetChipHolder.pop();
            if (!dontComeRegionBetChipHolder.empty()) {
                dontComeRegionBetChipHolder.peek();
            }
            removeDontComeBetFromRegion();
        }else if (previousRegion.equals(findViewById(R.id.comeBetRegion))) {
            comeRegionBetChipHolder.pop();
            if (!comeRegionBetChipHolder.empty()) {
                comeRegionBetChipHolder.peek();
            }
            removeComeBetFromRegion();
        }else if (previousRegion.equals(findViewById(R.id.passBetRegion))) {
            passRegionBetChipHolder.pop();
            if (!passRegionBetChipHolder.empty()) {
                passRegionBetChipHolder.peek();
            }
            removePassBetFromRegion();
        }else if (previousRegion.equals(findViewById(R.id.dontPassBetRegion))) {
            dontPassRegionBetChipHolder.pop();
            if (!dontPassRegionBetChipHolder.empty()) {
                dontPassRegionBetChipHolder.peek();
            }
            removeDontPassBetFromRegion();
        }else if (previousRegion.equals(findViewById(R.id.lay10))) {

        }else if (previousRegion.equals(findViewById(R.id.lay9))) {

        }else if (previousRegion.equals(findViewById(R.id.lay8))) {

        }else if (previousRegion.equals(findViewById(R.id.lay6))) {

        }else if (previousRegion.equals(findViewById(R.id.lay5))) {

        }else if (previousRegion.equals(findViewById(R.id.lay4))) {

        }else if (previousRegion.equals(findViewById(R.id.point10))) {

        }else if (previousRegion.equals(findViewById(R.id.point9))) {

        }else if (previousRegion.equals(findViewById(R.id.point8))) {

        }else if (previousRegion.equals(findViewById(R.id.point6))) {

        }else if (previousRegion.equals(findViewById(R.id.point5))) {

        }else if (previousRegion.equals(findViewById(R.id.point4))) {

        }else if (previousRegion.equals(findViewById(R.id.place10))) {

        }else if (previousRegion.equals(findViewById(R.id.place9))) {

        }else if (previousRegion.equals(findViewById(R.id.place8))) {

        }else if (previousRegion.equals(findViewById(R.id.place6))) {

        }else if (previousRegion.equals(findViewById(R.id.place5))) {

        }else if (previousRegion.equals(findViewById(R.id.place4))) {

        }

    }

    public View getSelectedChip() {
        if (chipSelected == 1) {
            selectedChip = findViewById(R.id.chip1ImageView);
        } else if (chipSelected == 5) {
            selectedChip = findViewById(R.id.chip5ImageView);
        }else if (chipSelected == 25) {
            selectedChip = findViewById(R.id.chip25ImageView);
        }else if (chipSelected == 100) {
            selectedChip = findViewById(R.id.chip100ImageView);
        }else if (chipSelected == 500) {
            selectedChip = findViewById(R.id.chip500ImageView);
        }
        return selectedChip;
    }

    static private enum Status { GAMEOVER, START };

    TextView rollResult;
    static TextView moneyInfo;
    static TextView betTotalText;
    TextView pointText;

    ImageView chip1ImageView, chip5ImageView, chip25ImageView,
            chip100ImageView, chip500ImageView, dice1ImageView, dice2ImageView;

    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);

        //Remove notification bar
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_main);
        findViewById(R.id.dontComeBetRegion).setOnDragListener(this);
        findViewById(R.id.comeBetRegion).setOnDragListener(this);
        findViewById(R.id.fieldsBetRegion).setOnDragListener(this);
        findViewById(R.id.passBetRegion).setOnDragListener(this);
        findViewById(R.id.dontPassBetRegion).setOnDragListener(this);

        findViewById(R.id.lay4).setOnDragListener(this);
        findViewById(R.id.lay5).setOnDragListener(this);
        findViewById(R.id.lay6).setOnDragListener(this);
        findViewById(R.id.lay8).setOnDragListener(this);
        findViewById(R.id.lay9).setOnDragListener(this);
        findViewById(R.id.lay10).setOnDragListener(this);

        findViewById(R.id.point4).setOnDragListener(this);
        findViewById(R.id.point5).setOnDragListener(this);
        findViewById(R.id.point6).setOnDragListener(this);
        findViewById(R.id.point8).setOnDragListener(this);
        findViewById(R.id.point9).setOnDragListener(this);
        findViewById(R.id.point10).setOnDragListener(this);

        findViewById(R.id.place4).setOnDragListener(this);
        findViewById(R.id.place5).setOnDragListener(this);
        findViewById(R.id.place6).setOnDragListener(this);
        findViewById(R.id.place8).setOnDragListener(this);
        findViewById(R.id.place9).setOnDragListener(this);
        findViewById(R.id.place10).setOnDragListener(this);

        addListenerOnImageViews();

        rollResult = (TextView) findViewById(R.id.rollResult);
        betTotalText = (TextView)findViewById(R.id.betTotal);
        moneyInfo = (TextView)findViewById(R.id.moneyInfo);
        pointText = (TextView)findViewById(R.id.pointText);
        dice1ImageView = (ImageView) findViewById(R.id.dice1ImageView);
        dice2ImageView = (ImageView) findViewById(R.id.dice2ImageView);

        gameStatus = Status.START;
        hasBet = false;
        money = 500;
        totalBet = 0;
        chipSelected = 0;

        moneyInfo.setText("Money: $" + String.valueOf(money));
        betTotalText.setText("Total Bet: $0");

        final ImageView chip1ImageView = (ImageView) findViewById(R.id.chip1ImageView);
        chip1ImageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                chip1ImageView.setPadding(10,10,10,10);
                chipSelected = 1;
            }
        });

        Button rollDiceButton = (Button) findViewById(R.id.rollDiceButton);
        rollDiceButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                checkMoney();
                if (!gameStatus.equals(Status.GAMEOVER)) {
                    playCraps();
                } else {
                    rollResult.setText("You are out of money!");
                }
                moneyInfo.setText("Money: $" + String.valueOf(money));
            }
        });

        final ViewGroup dontPassBetRegion = (ViewGroup) findViewById(R.id.dontPassBetRegion);
        dontPassBetRegion.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                addSelectedChipToRegion(v, dontPassBetRegion);
                dontPassBetRegionActivity();
            }
        });

        final ViewGroup passBetRegion = (ViewGroup) findViewById(R.id.passBetRegion);
        passBetRegion.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                addSelectedChipToRegion(v, passBetRegion);
                passBetRegionActivity();
            }
        });


        final ViewGroup fieldsBetRegion = (ViewGroup) findViewById(R.id.fieldsBetRegion);
        fieldsBetRegion.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                addSelectedChipToRegion(v, fieldsBetRegion);
                fieldsBetActivity();
            }
        });

        final ViewGroup dontComeBetRegion = (ViewGroup) findViewById(R.id.dontComeBetRegion);
        dontComeBetRegion.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                addSelectedChipToRegion(v, dontComeBetRegion);
                fieldsBetActivity();
            }
        });

        final ViewGroup ComeBetRegion = (ViewGroup) findViewById(R.id.comeBetRegion);
        ComeBetRegion.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                addSelectedChipToRegion(v, ComeBetRegion);
                fieldsBetActivity();
            }
        });

        final ViewGroup lay4 = (ViewGroup) findViewById(R.id.lay4);
        lay4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                addSelectedChipToRegion(v, lay4);
                fieldsBetActivity();
            }
        });

        final ViewGroup lay5 = (ViewGroup) findViewById(R.id.lay5);
        lay5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                addSelectedChipToRegion(v, lay5);
                fieldsBetActivity();
            }
        });

        final ViewGroup lay6 = (ViewGroup) findViewById(R.id.lay6);
        lay6.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                addSelectedChipToRegion(v, lay6);
                fieldsBetActivity();
            }
        });

        final ViewGroup lay8 = (ViewGroup) findViewById(R.id.lay8);
        lay8.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                addSelectedChipToRegion(v, lay8);
                fieldsBetActivity();
            }
        });

        final ViewGroup lay9 = (ViewGroup) findViewById(R.id.lay9);
        lay9.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                addSelectedChipToRegion(v, lay9);
                fieldsBetActivity();
            }
        });

        final ViewGroup lay10 = (ViewGroup) findViewById(R.id.lay10);
        lay10.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                addSelectedChipToRegion(v, lay10);
                fieldsBetActivity();
            }
        });

        final ViewGroup point4 = (ViewGroup) findViewById(R.id.point4);
        point4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                addSelectedChipToRegion(v, point4);
                fieldsBetActivity();
            }
        });

        final ViewGroup point5 = (ViewGroup) findViewById(R.id.point5);
        point5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                addSelectedChipToRegion(v, point5);
                fieldsBetActivity();
            }
        });

        final ViewGroup point6 = (ViewGroup) findViewById(R.id.point6);
        point6.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                addSelectedChipToRegion(v, point6);
                fieldsBetActivity();
            }
        });

        final ViewGroup point8 = (ViewGroup) findViewById(R.id.point8);
        point8.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                addSelectedChipToRegion(v, point8);
                fieldsBetActivity();
            }
        });

        final ViewGroup point9 = (ViewGroup) findViewById(R.id.point9);
        point9.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                addSelectedChipToRegion(v, point9);
                fieldsBetActivity();
            }
        });

        final ViewGroup point10 = (ViewGroup) findViewById(R.id.point10);
        point10.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                addSelectedChipToRegion(v, point10);
                fieldsBetActivity();
            }
        });

        final ViewGroup place4 = (ViewGroup) findViewById(R.id.place4);
        place4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                addSelectedChipToRegion(v, place4);
                fieldsBetActivity();
            }
        });

        final ViewGroup place5 = (ViewGroup) findViewById(R.id.place5);
        place5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                addSelectedChipToRegion(v, place5);
                fieldsBetActivity();
            }
        });

        final ViewGroup place6 = (ViewGroup) findViewById(R.id.place6);
        place6.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                addSelectedChipToRegion(v, place6);
                fieldsBetActivity();
            }
        });

        final ViewGroup place8 = (ViewGroup) findViewById(R.id.place8);
        place8.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                addSelectedChipToRegion(v, place8);
                fieldsBetActivity();
            }
        });

        final ViewGroup place9 = (ViewGroup) findViewById(R.id.place9);
        place9.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                addSelectedChipToRegion(v, place9);
                fieldsBetActivity();
            }
        });

        final ViewGroup place10 = (ViewGroup) findViewById(R.id.place10);
        place10.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                addSelectedChipToRegion(v, place10);
                fieldsBetActivity();
            }
        });

        Button resetButton = (Button) findViewById(R.id.resetButton);
        resetButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                reset();
            }
        });
    }

    private void addSelectedChipToRegion(View v, ViewGroup vg) {

        v = getSelectedChip();
        ViewGroup container = vg;

        // Added the following to copy the old view's bitmap to a new ImageView:
        ImageView oldView = (ImageView) v;
        ImageView newView = new ImageView(getApplicationContext());

        newView.setBackground(oldView.getBackground());

        if (container.equals(findViewById(R.id.fieldsBetRegion))) {
            if(!hasFieldsBet) {
                fieldsRegionBetChipHolder.push(newView);
                for (int i = 0; i < 6; i++) {
                    if (i == 5) {
                        container.addView(fieldsRegionBetChipHolder.peek());
                    } else {
                        ImageView blankView = new ImageView(getApplicationContext());
                        blankView.setBackground(oldView.getBackground());
                        blankView.setVisibility(View.INVISIBLE);
                        container.addView(blankView);
                    }
                }
                newView.setLongClickable(true);
                newView.setOnTouchListener(this);
            }else if(hasFieldsBet) {
                fieldsRegionBetChipHolder.push(newView);
                newView.setLongClickable(true);
                newView.setOnTouchListener(this);
            }
        }else if (container.equals(findViewById(R.id.dontComeBetRegion))) {

        }else if (container.equals(findViewById(R.id.comeBetRegion))) {

        }else if (container.equals(findViewById(R.id.passBetRegion))) {
            if (!hasPoint && !hasPassBetRegion && !hasPassOddsBet) {
                passRegionBetChipHolder.push(newView);
                for (int i = 0; i < 6; i++) {
                    if (i == 5) {
                        container.addView(passRegionBetChipHolder.peek());
                    } else {
                        ImageView blankView = new ImageView(getApplicationContext());
                        blankView.setBackground(oldView.getBackground());
                        blankView.setVisibility(View.INVISIBLE);
                        container.addView(blankView);
                    }
                }
                newView.setLongClickable(true);
                newView.setOnTouchListener(this);
            } else if (!hasPoint && hasPassBetRegion && !hasPassOddsBet) {
                passRegionBetChipHolder.push(newView);
                newView.setLongClickable(true);
                newView.setOnTouchListener(this);
            }else if (hasPoint && hasPassBetRegion && !hasPassOddsBet) {
                passRegionOddsBetChipHolder.push(newView);
                container.addView(passRegionOddsBetChipHolder.peek());
                newView.setLongClickable(true);
                newView.setOnTouchListener(this);
            }else if (hasPoint && hasPassBetRegion && hasPassOddsBet) {
                passRegionOddsBetChipHolder.push(newView);
                newView.setLongClickable(true);
                newView.setOnTouchListener(this);
            }
        }else if (container.equals(findViewById(R.id.dontPassBetRegion))) {
            if (!hasPoint && !hasDontPassBetRegion && !hasDontPassOddsBet) {
                dontPassRegionBetChipHolder.push(newView);
                for (int i = 0; i < 6; i++) {
                    if (i == 5) {
                        container.addView(dontPassRegionBetChipHolder.peek());
                    } else {
                        ImageView blankView = new ImageView(getApplicationContext());
                        blankView.setBackground(oldView.getBackground());
                        blankView.setVisibility(View.INVISIBLE);
                        container.addView(blankView);
                    }
                }
                newView.setLongClickable(true);
                newView.setOnTouchListener(this);
            } else if (!hasPoint && hasDontPassBetRegion && !hasDontPassOddsBet) {
                dontPassRegionBetChipHolder.push(newView);
                newView.setLongClickable(true);
                newView.setOnTouchListener(this);
            }else if (hasPoint && hasDontPassBetRegion && !hasDontPassOddsBet) {
                dontPassRegionOddsBetChipHolder.push(newView);
                container.addView(passRegionOddsBetChipHolder.peek());
                newView.setLongClickable(true);
                newView.setOnTouchListener(this);
            }else if (hasPoint && hasDontPassBetRegion && hasDontPassOddsBet) {
                dontPassRegionOddsBetChipHolder.push(newView);
                newView.setLongClickable(true);
                newView.setOnTouchListener(this);
            }

        }else if (container.equals(findViewById(R.id.lay10))) {

        }else if (container.equals(findViewById(R.id.lay9))) {

        }else if (container.equals(findViewById(R.id.lay8))) {

        }else if (container.equals(findViewById(R.id.lay6))) {

        }else if (container.equals(findViewById(R.id.lay5))) {

        }else if (container.equals(findViewById(R.id.lay4))) {

        }else if (container.equals(findViewById(R.id.point10))) {

        }else if (container.equals(findViewById(R.id.point9))) {

        }else if (container.equals(findViewById(R.id.point8))) {

        }else if (container.equals(findViewById(R.id.point6))) {

        }else if (container.equals(findViewById(R.id.point5))) {

        }else if (container.equals(findViewById(R.id.point4))) {

        }else if (container.equals(findViewById(R.id.place10))) {

        }else if (container.equals(findViewById(R.id.place9))) {

        }else if (container.equals(findViewById(R.id.place8))) {

        }else if (container.equals(findViewById(R.id.place6))) {

        }else if (container.equals(findViewById(R.id.place5))) {

        }else if (container.equals(findViewById(R.id.place4))) {

        }
    }

    private void passBetRegionActivity() {
        checkMoney();

        if (chipSelected != 0) {
            hasBet = true;
            hasPassBetRegion = true;
            if (!hasPoint) {
                passBetRegion = chipSelected;
                if (((money - passBetRegion) >= 0)) {
                    passBetRegion();
                } else {
                    rollResult.setText("You are out of money!");
                }
            } else {
                passOddsBet = chipSelected;
                hasPassOddsBet = true;
                if ((money - passOddsBet) >= 0) {
                    oddsPassBetRegion();
                }
            }
        } else {
            rollResult.setText("Must select a chip to bet");
        }
        betTotalText.setText("Total Bet: $" + String.valueOf(totalBet));
        moneyInfo.setText("Money: $" + String.valueOf(money));
    }

    public void removePassBetFromRegion() {
        passBetRegion -= chipSelected;
        totalPassBetRegion -= chipSelected;
        money += passBetRegion;
        totalBet -= chipSelected;
        if (totalPassBetRegion == 0) {
            hasPassBetRegion = false;
            removeAllChipsFromPass();
        }
    }

    private void dontPassBetRegionActivity() {
        checkMoney();

        if (chipSelected != 0) {
            hasBet = true;
            hasDontPassBetRegion = true;
            dontPassBetRegion = chipSelected;
            if (!hasPoint) {
                if ((money - dontPassBetRegion) >= 0) {
                    dontPassBetRegion();

                } else {
                    rollResult.setText("You are out of money!");
                }
            }else {
                hasDontPassOddsBet = true;
                if ((money - dontPassOddsBet) >= 0) {
                    oddsDontPassBetRegion();
                }
            }
        } else {
            rollResult.setText("Must select a chip to bet");
        }

        betTotalText.setText("Total Bet: $" + String.valueOf(totalBet));
        moneyInfo.setText("Money: $" + String.valueOf(money));
    }

    public void removeDontPassBetFromRegion() {
        dontPassBetRegion -= chipSelected;
        totalDontPassBetRegion -= chipSelected;
        money += dontPassBetRegion;
        totalBet -= chipSelected;
        if (totalDontPassBetRegion == 0) {
            hasDontPassBetRegion = false;
            removeAllChipsFromDontPass();
        }
    }

    public void fieldsBetActivity() {
        checkMoney();
        if (chipSelected != 0) {
            hasBet = true;
            hasFieldsBet = true;
            fieldsBet = chipSelected;
            if ((money - fieldsBet) >= 0) {
                fieldsBet();
            } else {
                rollResult.setText("You are out of money!");
            }
        } else {
            rollResult.setText("Must select a chip to bet");
        }
        betTotalText.setText("Total Bet: $" + String.valueOf(totalBet));
        moneyInfo.setText("Money: $" + String.valueOf(money));
    }

    public void removeFieldBetFromRegion() {
        fieldsBet -= chipSelected;
        totalFieldsBet -= chipSelected;
        money += chipSelected;
        totalBet -= chipSelected;
        if (totalFieldsBet == 0) {
            hasFieldsBet = false;
            removeAllChipsFromFields();
        }
    }

    public void ComeBetActivity() {
        checkMoney();
        if (chipSelected != 0) {
            hasBet = true;
            hasComeBet = true;
            fieldsBet = chipSelected;
            if ((money - fieldsBet) >= 0) {
                fieldsBet();
            } else {
                rollResult.setText("You are out of money!");
            }
        } else {
            rollResult.setText("Must select a chip to bet");
        }
        betTotalText.setText("Total Bet: $" + String.valueOf(totalBet));
        moneyInfo.setText("Money: $" + String.valueOf(money));

    }

    public void removeComeBetFromRegion() {
        comeBet -= chipSelected;
        totalComeBetRegion -= chipSelected;
        money += comeBet;
        totalBet -= chipSelected;
        if (totalComeBetRegion == 0) {
            hasComeBet = false;
            removeAllChipsFromCome();
        }
    }

    public void DontComeBetActivity() {
        checkMoney();

    }

    public void removeDontComeBetFromRegion() {
        dontComeBet -= chipSelected;
        totalDontComeBetRegion -= chipSelected;
        money -= dontComeBet;
        totalBet -= chipSelected;
        if (totalDontComeBetRegion == 0) {
            hasDontComeBet = false;
            removeAllChipsFromDontCome();
        }
    }

    private void removeAllChipsFromDontCome() {
        ViewGroup vg = (ViewGroup) findViewById(R.id.dontComeBetRegion);
        dontComeRegionBetChipHolder.removeAllElements();
        dontComeRegionOddsBetChipHolder.removeAllElements();
        vg.removeAllViews();
    }

    private void removeAllChipsFromCome() {
        ViewGroup vg = (ViewGroup) findViewById(R.id.comeBetRegion);
        comeRegionBetChipHolder.removeAllElements();
        comeRegionOddsBetChipHolder.removeAllElements();
        vg.removeAllViews();
    }

    private void removeAllChipsFromPass() {
        ViewGroup vg = (ViewGroup) findViewById(R.id.passBetRegion);
        passRegionBetChipHolder.removeAllElements();
        passRegionOddsBetChipHolder.removeAllElements();
        vg.removeAllViews();
    }

    private void removeAllChipsFromDontPass() {
        ViewGroup vg = (ViewGroup) findViewById(R.id.dontPassBetRegion);
        dontPassRegionBetChipHolder.removeAllElements();
        dontPassRegionOddsBetChipHolder.removeAllElements();
        vg.removeAllViews();
    }

    private void removeAllChipsFromFields() {
        ViewGroup vg = (ViewGroup) findViewById(R.id.fieldsBetRegion);
        fieldsRegionBetChipHolder.removeAllElements();
        vg.removeAllViews();
    }

    private void addListenerOnImageViews() {
        findViewById(R.id.chip1ImageView).setOnTouchListener(this);
        findViewById(R.id.chip5ImageView).setOnTouchListener(this);
        findViewById(R.id.chip25ImageView).setOnTouchListener(this);
        findViewById(R.id.chip100ImageView).setOnTouchListener(this);
        findViewById(R.id.chip500ImageView).setOnTouchListener(this);
    }

    private void playCraps() {

        if(!hasPoint){
            pointText.setText("The Point is: No Point");
        }

        //Roll the dice
        sumOfDice = rollDice(sum);
        if (sumOfDice == 7) {
            removeAllChipsFromBoard();
        }

        if (Boolean.valueOf(hasFieldsBet)) {
            checkFieldsBet();
        }

        if (Boolean.valueOf(comeOutRoll)) {
            checkComeOutBets();
            checkForPoint();
        } else {
            checkPointBets();
        }

        if (!hasDontPassBetRegion && !hasPassBetRegion && !hasFieldsBet) {
            hasBet = false;
            totalBet = 0;
            betTotalText.setText("Total Bet: 0");
        }

        betTotalText.setText("Total Bet: $" + String.valueOf(totalBet));
        moneyInfo.setText("Money: $" + String.valueOf(money));
    }

    private void checkForPoint() {
        if (Arrays.asList(pointValues).contains(sumOfDice)) {
            point = sumOfDice;
            hasPoint = true;
            comeOutRoll = false; //point is established
            pointText.setText("The Point is: " + String.valueOf(point) +"\n");
        }
    }

    private void checkMoney() {
        //These set of conditionals define what happens on exit.
        if (money < 0){
            gameStatus = Status.GAMEOVER;
        }
    }

    private void oddsDontPassBetRegion() {
        money = money - dontPassOddsBet;
        hasDontPassOddsBet = true;
        totalBet += dontPassOddsBet;
        totalPassOddsBet += dontPassOddsBet;
        rollResult.setText("Dont Pass Odds Bet for $" + String.valueOf(dontPassBetRegion) + "\n");
    }

    private void oddsPassBetRegion() {
        money = money - passOddsBet;
        totalBet += passOddsBet;
        totalPassOddsBet += passOddsBet;
        rollResult.setText("Bet Pass Odds for $" + String.valueOf(dontPassOddsBet) + "\n");
    }

    private void dontPassBetRegion() {
        money -= dontPassBetRegion;
        totalBet += dontPassBetRegion;
        totalDontPassBetRegion += dontPassBetRegion;
        rollResult.setText("Dont Pass Bet for $" + String.valueOf(dontPassBetRegion) + "\n");
    }

    private void passBetRegion() {
        money -= passBetRegion;
        totalBet += passBetRegion;
        totalPassBetRegion += passBetRegion;
        rollResult.setText("Pass Bet for $" + String.valueOf(passBetRegion) + "\n");
    }

    private void fieldsBet() {
        money -= fieldsBet;
        totalFieldsBet += fieldsBet;
        totalBet += fieldsBet;
        rollResult.setText("Fields Bet for $" + String.valueOf(fieldsBet) + "\n");
    }

    private void checkComeOutBets() {

        if(hasDontPassBetRegion) {
            checkComeOutDontPassBetRegion();
        }

        if(hasPassBetRegion) {
            checkComeOutPassBetRegion();
        }
    }

    private void checkPointBets() {

        if(hasDontPassBetRegion) {
            checkPointDontPassBetRegion();
        }

        if(hasPassBetRegion) {
            checkPointPassBetRegion();
        }

    }

    private void checkPointPassBetRegion() {
        if ( sumOfDice == point ){    // win by hitting point
            double payOut = 0;
            if(point == 4 || point == 10){
                payOut = totalPassOddsBet + roundTwoDecimals((totalPassOddsBet*(2d/1d)));
            }else if(point == 5 || point == 9) {
                payOut = totalPassOddsBet + roundTwoDecimals((totalPassOddsBet*(3d/2d)));
            }else if(point == 6 || point == 8) {
                payOut = totalPassOddsBet + roundTwoDecimals((totalPassOddsBet*(6d/5d)));
            }
            rollResult.setText("You Won on the Pass Bet!\n");
            money = money + (totalPassBetRegion*2)+ payOut;
            hasPassOddsBet = false;
            hasPassBetRegion = false;
            hasPoint = false;
            comeOutRoll = true;
            totalPassBetRegion = 0;
            totalDontPassBetRegion = 0;
            totalPassOddsBet = 0;
            totalDontPassOddsBet = 0;
            totalBet -= passBetRegion;

        }else {
            if ( sumOfDice == 7 ){       // lose by rolling 7
                hasPassOddsBet = false;
                hasPassBetRegion = false;
                hasPoint = false;
                comeOutRoll = true;
                rollResult.setText("You Lost on the Pass Bet!\n");
                totalPassBetRegion = 0;
                totalPassOddsBet = 0;
                totalBet -= passBetRegion;
            }
        }
    }

    private void checkPointDontPassBetRegion() {
        if ( sumOfDice == point ){    // lose by hitting point
            hasDontPassOddsBet = false;
            hasDontPassBetRegion = false;
            hasPoint = false;
            comeOutRoll = true;
            totalDontPassBetRegion = 0;
            totalDontPassOddsBet = 0;
            totalBet -= dontPassBetRegion;
            rollResult.setText("You Lost on the Dont Pass!\n");

        }else {
            if ( sumOfDice == 7 ){       // win by rolling 7
                double payOut = 0;
                if(point == 4 || point == 10){
                    payOut = totalDontPassOddsBet + roundTwoDecimals((totalDontPassOddsBet*(1d/2d)));
                }else if(point == 5 || point == 9) {
                    payOut = totalDontPassOddsBet + roundTwoDecimals((totalDontPassOddsBet*(2d/3d)));
                }else if(point == 6 || point == 8) {
                    payOut = totalDontPassOddsBet + roundTwoDecimals((totalDontPassOddsBet*(5d/6d)));
                }
                money = money + (totalDontPassBetRegion*2) + payOut;
                hasDontPassOddsBet = false;
                hasDontPassBetRegion = false;
                hasPoint = false;
                comeOutRoll = true;
                rollResult.setText("You Won on the Dont Pass!\n");
                totalDontPassBetRegion = 0;
                totalDontPassOddsBet = 0;
                totalBet -= dontPassBetRegion;

            }
        }
    }

    private void checkFieldsBet() {
        if (Arrays.asList(fieldValues).contains(sumOfDice)) {
            if (sumOfDice == 2) {
                money = money + (totalFieldsBet + (totalFieldsBet * 2));
                rollResult.setText("You won double the Fields bet!\n");
            } else if (sumOfDice == 12) {
                money = money + (totalFieldsBet + (totalFieldsBet * 2));
                rollResult.setText("You won double the Fields bet!\n");
            }else{
                money += (totalFieldsBet*2);
                rollResult.setText("You won the Fields bet!\n");
            }
            //Reset the fields Bet
            hasFieldsBet = false;
            totalBet -= totalFieldsBet;
            totalFieldsBet = 0;
            fieldsBet = 0;
            //fieldsChipSelectedImage.setVisibility(View.GONE);
        }else {
            //Lost All Fields Bet
            rollResult.setText("You lost the Fields bet!\n");
            //Reset the fields Bet
            hasFieldsBet = false;
            totalBet -= totalFieldsBet;
            totalFieldsBet = 0;
            fieldsBet = 0;
            ViewGroup vg = (ViewGroup) findViewById(R.id.fieldsBetRegion);
            vg.removeAllViews();

        }

    }

    private int rollDice(int sum) {
        // pick random die values
        die1 = 1 + randomNumbers.nextInt( 6 ); // first die roll
        die2 = 1 + randomNumbers.nextInt( 6 ); // second die roll
        sum = die1 + die2; // sum of die values

        if(die1 == 1){
            dice1ImageView.setBackgroundResource(R.drawable.dice1);
        }else if(die1 == 2){
            dice1ImageView.setBackgroundResource(R.drawable.dice2);
        }else if(die1 == 3){
            dice1ImageView.setBackgroundResource(R.drawable.dice3);
        }else if(die1 == 4){
            dice1ImageView.setBackgroundResource(R.drawable.dice4);
        }else if(die1 == 5){
            dice1ImageView.setBackgroundResource(R.drawable.dice5);
        }else if(die1 == 6){
            dice1ImageView.setBackgroundResource(R.drawable.dice6);
        }

        if(die2 == 1){
            dice2ImageView.setBackgroundResource(R.drawable.dice1);
        }else if(die2 == 2){
            dice2ImageView.setBackgroundResource(R.drawable.dice2);
        }else if(die2 == 3){
            dice2ImageView.setBackgroundResource(R.drawable.dice3);
        }else if(die2 == 4){
            dice2ImageView.setBackgroundResource(R.drawable.dice4);
        }else if(die2 == 5){
            dice2ImageView.setBackgroundResource(R.drawable.dice5);
        }else if(die2 == 6){
            dice2ImageView.setBackgroundResource(R.drawable.dice6);
        }

        return sum; // return sum of dice
    }

    private void reset() {

        removeAllChipsFromBoard();


        gameStatus = Status.START;
        hasBet = false;
        money = 500;
        totalBet = 0;
        fieldsBet = 0;
        totalFieldsBet = 0;
        passOddsBet = 0;
        passBetRegion = 0;
        passBetRegion = 0;
        dontPassBetRegion = 0;
        dontPassOddsBet = 0;
        point = 0;
        hasDontPassBetRegion = false;
        hasPassBetRegion = false;
        hasFieldsBet = false;
        hasPoint = false;
        comeOutRoll = true;

        moneyInfo.setText("Money: $" + String.valueOf(money));
        betTotalText.setText("Total Bet: $0");
        pointText.setText("No Point!");
        rollResult.setText("Game Reset");

    }

    private void removeAllChipsFromBoard() {
        ViewGroup vg = (ViewGroup) findViewById(R.id.passBetRegion);
        passRegionBetChipHolder.removeAllElements();
        passRegionOddsBetChipHolder.removeAllElements();
        vg.removeAllViews();

        vg = (ViewGroup) findViewById(R.id.dontPassBetRegion);
        dontPassRegionBetChipHolder.removeAllElements();
        dontPassRegionOddsBetChipHolder.removeAllElements();
        vg.removeAllViews();

        vg = (ViewGroup) findViewById(R.id.fieldsBetRegion);
        fieldsRegionBetChipHolder.removeAllElements();
        vg.removeAllViews();

        vg = (ViewGroup) findViewById(R.id.comeBetRegion);
        comeRegionBetChipHolder.removeAllElements();
        comeRegionOddsBetChipHolder.removeAllElements();
        vg.removeAllViews();

        vg = (ViewGroup) findViewById(R.id.dontComeBetRegion);
        dontComeRegionBetChipHolder.removeAllElements();
        dontComeRegionOddsBetChipHolder.removeAllElements();
        vg.removeAllViews();

        vg = (ViewGroup) findViewById(R.id.lay4);
        vg.removeAllViews();
        vg = (ViewGroup) findViewById(R.id.lay5);
        vg.removeAllViews();
        vg = (ViewGroup) findViewById(R.id.lay6);
        vg.removeAllViews();
        vg = (ViewGroup) findViewById(R.id.lay8);
        vg.removeAllViews();
        vg = (ViewGroup) findViewById(R.id.lay9);
        vg.removeAllViews();
        vg = (ViewGroup) findViewById(R.id.lay10);
        vg.removeAllViews();

        vg = (ViewGroup) findViewById(R.id.point4);
        vg.removeAllViews();
        vg = (ViewGroup) findViewById(R.id.point5);
        vg.removeAllViews();
        vg = (ViewGroup) findViewById(R.id.point6);
        vg.removeAllViews();
        vg = (ViewGroup) findViewById(R.id.point8);
        vg.removeAllViews();
        vg = (ViewGroup) findViewById(R.id.point9);
        vg.removeAllViews();
        vg = (ViewGroup) findViewById(R.id.point10);
        vg.removeAllViews();

        vg = (ViewGroup) findViewById(R.id.place4);
        vg.removeAllViews();
        vg = (ViewGroup) findViewById(R.id.place5);
        vg.removeAllViews();
        vg = (ViewGroup) findViewById(R.id.place6);
        vg.removeAllViews();
        vg = (ViewGroup) findViewById(R.id.place8);
        vg.removeAllViews();
        vg = (ViewGroup) findViewById(R.id.place9);
        vg.removeAllViews();
        vg = (ViewGroup) findViewById(R.id.place10);
        vg.removeAllViews();
    }

    private void checkComeOutDontPassBetRegion() {
        if (sumOfDice == 2 || sumOfDice == 3) {
            money = money + roundTwoDecimals(totalDontPassBetRegion * 2);
            rollResult.setText("You won on the Come out Roll!\n");
            hasDontPassBetRegion = false;
            totalDontPassBetRegion = 0;
        }else if(sumOfDice == 12) {
            //No money is lost, 12 is push

        }else if (sumOfDice == 7 || sumOfDice == 11){
            rollResult.setText("You lost on the Come out Roll!\n");
            hasDontPassBetRegion = false;
            totalDontPassBetRegion = 0;
        }
        hasPoint = false;
        comeOutRoll = true;
    }

    private void checkComeOutPassBetRegion() {
        if (sumOfDice == 7 || sumOfDice == 11) {
            money = money + roundTwoDecimals(totalPassBetRegion * 2);
            rollResult.setText("You won on the Come out Roll!\n");
            hasPassBetRegion = false;
            totalPassBetRegion = 0;
        }else if (sumOfDice == 2 || sumOfDice == 3 || sumOfDice == 12 ) {
            rollResult.setText("You lost on the Come out Roll!\n");
            hasPassBetRegion = false;
            totalPassBetRegion = 0;
        }
        hasPoint = false;
        comeOutRoll = true;
    }

    double roundTwoDecimals(double d) {
        DecimalFormat twoDForm = new DecimalFormat("#.##");
        return Double.valueOf(twoDForm.format(d));
    }
}
